﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using ZhiHeng.Consume.DataContract;
using ZhiHeng.OneCard.CommUtility;
using System.Linq;

namespace ZhiHeng.OneCard.WpfClient
{
    public static class EntityExtension
    {

        public static int ExtAdd<T>(this T t) where T : EntityObject
        {
            var client = new ActionService.ActionServiceClient();
            try
            {
                return client.Add(typeof(T).Name, SerializationHelper.DataContractSerialize(t));
            }
            finally
            {
                client.Close();
            }
        }

        public static int ExtDelete<T>(this T t) where T : EntityObject
        {
            var client = new ActionService.ActionServiceClient();
            try
            {
                return client.Delete(typeof(T).Name, SerializationHelper.DataContractSerialize(t));
            }
            finally
            {
                client.Close();
            }
        }

        public static int ExtUpdate<T>(this T t) where T : EntityObject
        {
            var client = new ActionService.ActionServiceClient();
            try
            {
                return client.Update(typeof(T).Name,SerializationHelper.DataContractSerialize(t) ); 
            }
            finally
            {
                client.Close();
            }
        }

        public static long ExtCount<T>(
            this T entity
            , string predicate = ""
            , params SearchParameter[] parameters) where T : EntityObject
        {
            var client = new ActionService.ActionServiceClient();
            try
            { 
                string filter = SerializationHelper.XmlSerialize(parameters);

                return client.Count(typeof(T).Name,  predicate, filter); 
            }
            finally
            {
                client.Close();
            }
        }

        /// <summary>
        /// Get entity list from database
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="loadProperty">if load asociate objects</param>
        /// <param name="predicate"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static List<T> ExtGetAll<T>(
            this T entity
            , bool loadProperty = false
            , string predicate = ""
            , params SearchParameter[] parameters ) where T : EntityObject
        {
            var client = new ActionService.ActionServiceClient();
            try
            {
                //.GetAll("it.StName=@P1", new ObjectParameter("P1", "N1")); 
                string filter = SerializationHelper.XmlSerialize(parameters);

                var xml = client.GetAll(typeof(T).Name, loadProperty, predicate, filter);
                if (string.IsNullOrEmpty(xml))
                {
                    return null;
                }
                var y = new List<T>();
                y = loadProperty
                        ? SerializationHelper.DataContractDeserialize<List<T>>(xml)
                        : SerializationHelper.XmlDeserialize<List<T>>(xml);
                return y;

            }
            finally
            {
                client.Close();
            }
        }

        public static T ExtGetSingle<T>(
          this T entity
            , string predicate 
          , bool loadProperty = false
            , params SearchParameter[] parameters ) where T : EntityObject
        {
            var client = new ActionService.ActionServiceClient();
            try
            {
                string filter = SerializationHelper.XmlSerialize(parameters);
                var xml = client.GetSingle(typeof(T).Name, loadProperty, predicate, filter);
                if (string.IsNullOrEmpty(xml))
                {
                    return null;
                }
                var y = loadProperty
                        ? SerializationHelper.DataContractDeserialize<T>(xml)
                        : SerializationHelper.XmlDeserialize<T>(xml); 
                return y;
            }
            finally
            {
                client.Close();
            }
        }
    }
}
